#include "widget.h"
#include "mythread.h"

#include <QApplication>
#include <QThread>
#include <QObject>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    //第一步：创建MyThread的对象
    MyThread *pMyTh = new MyThread;
    //第二步：创建QThread的对象
    QThread *qTh = new QThread;
    //第三步：将自定义类型对象的所有权转移给QThread类型的对象
    pMyTh->moveToThread(qTh);

    //连接信号与槽
    QObject::connect(qTh, &QThread::started, pMyTh, &MyThread::doTask);
    //连接该信号与槽的作用就是当任务doTask函数执行结束之后，触发信号，然后执行
    //QThread中的quit函数,让那个QThread从run方法中的事件循环中退出来
    QObject::connect(pMyTh, &MyThread::signalQuit, qTh, &QThread::quit);

    //为了回收MyThread创建的堆对象以及QThread创建的堆对象，可以利用信号与槽进行回收
    QObject::connect(qTh, &QThread::finished, pMyTh, &MyThread::deleteLater);
    QObject::connect(qTh, &QThread::finished, qTh, &QThread::deleteLater);

    QObject::connect(qTh, &QThread::finished, []{
        exit(0);
    });

    //第四步：启动QThread
    qTh->start();//启动QThread线程对象(它的底层会调用run方法)


    return a.exec();
}
